﻿using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using System;
using System.IO;
using WebMvc.Models;

namespace WebMvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //参考 https://github.com/aspnet/Security/issues/1310
            //services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores();
            services.AddAuthentication(options =>
                {
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddCookie(options =>
                {
                    options.ExpireTimeSpan = TimeSpan.FromHours(2);
                    options.LoginPath = new PathString("/Sys/User/Login/");
                    options.LogoutPath = new PathString("/Sys/User/Logoff/");
                    options.AccessDeniedPath = new PathString("/Sys/Home/AccessDenied/");
                    options.SlidingExpiration = true;
                });
            services.AddDbContextPool<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")));
            // Add framework services.
            services.AddMvc();
        }
        
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            // add NLog to ASP.NET Core
            loggerFactory.AddNLog();
            loggerFactory.ConfigureNLog("nlog.config");

            //使用静态文件，可以访问所有wwwroot目录下的静态文件
            app.UseStaticFiles();
            //使用静态文件，可以访问Views目录及子目录下的JS文件
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory())
            });

            

            app.UseAuthentication();

            //使用Mvc，设置默认路由
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                  );
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
